// vue-rander
const fs = require('fs');
const Vue = require('vue')
const renderer = require('vue-server-renderer').createRenderer()
module.exports = function vueRender(
  path
) {
  return function views(ctx, next) {
    if (ctx.render) return next()

    ctx.response.render = ctx.render = obj => {
		obj.template = fs.readFileSync(path + obj.template + '.vue', 'utf-8')
		const vueapp = new Vue(obj)
		return renderer.renderToString(vueapp).then(html => {
			ctx.body = html
		}, err => {
             new Error(`vue render error:`, err)
		})  
    }

    return next()
  }
}